{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 「第1章 画像分類」の準備ファイル\n",
    "\n",
    "- 本ファイルでは、第1章で使用するフォルダの作成とファイルのダウンロードを行います。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import urllib.request\n",
    "import zipfile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# フォルダ「data」が存在しない場合は作成する\n",
    "data_dir = \"./data/\"\n",
    "if not os.path.exists(data_dir):\n",
    "    os.mkdir(data_dir)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ImageNetのclass_indexをダウンロードする\n",
    "# Kerasで用意されているものです\n",
    "# https://github.com/fchollet/deep-learning-models/blob/master/imagenet_utils.py\n",
    "\n",
    "url = \"https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json\"\n",
    "save_path = os.path.join(data_dir, \"imagenet_class_index.json\")\n",
    "\n",
    "if not os.path.exists(save_path):\n",
    "    urllib.request.urlretrieve(url, save_path)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.3節で使用するアリとハチの画像データをダウンロードし解凍します\n",
    "# PyTorchのチュートリアルで用意されているものです\n",
    "# https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html\n",
    "\n",
    "url = \"https://download.pytorch.org/tutorial/hymenoptera_data.zip\"\n",
    "save_path = os.path.join(data_dir, \"hymenoptera_data.zip\")\n",
    "\n",
    "if not os.path.exists(save_path):\n",
    "    urllib.request.urlretrieve(url, save_path)\n",
    "\n",
    "    # ZIPファイルを読み込み\n",
    "    zip = zipfile.ZipFile(save_path)\n",
    "    zip.extractall(data_dir)  # ZIPを解凍\n",
    "    zip.close()  # ZIPファイルをクローズ\n",
    "\n",
    "    # ZIPファイルを消去\n",
    "    os.remove(save_path)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "【※（実施済み）】\n",
    "\n",
    "ゴールデンリトリバーの画像を手動でダウンロード\n",
    "\n",
    "https://pixabay.com/ja/photos/goldenretriever-%E7%8A%AC-3724972/\n",
    "の640×426サイズの画像\n",
    "（画像権利情報：CC0 Creative Commons、商用利用無料、帰属表示は必要ありません）\n",
    "\n",
    "を、フォルダ「data」の直下に置く。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以上"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
